﻿var lastFocusCtrl;

function paddZeros(number) {
    if ((number >= 0) && (number <= 9))
        return '0' + number.toString();
    else
        return number.toString();
}

function ensureFocus(c) {
    var controlSetPrefix = c.id.replace('_imgUp', '');
    controlSetPrefix = controlSetPrefix.replace('_imgDown', '');

    if (lastFocusCtrl == null) // This should only happen once, max
    {
        lastFocusCtrl = document.getElementById(controlSetPrefix + '_txtHour');
    }
    else {
        if (lastFocusCtrl.id.indexOf(controlSetPrefix) != 0) {
            var elem = document.getElementById(controlSetPrefix + '_txtHour')
            if (null != elem) {
                elem.focus();
                lastFocusCtrl = elem;
            }
        }
    }
}

function addTime(c, selectedTimeFormat, MinuteIncrement, SecondIncrement) {
    if (c != null)
        ensureFocus(c);
    var mainWordStartsAt = lastFocusCtrl.id.indexOf('_txt') + 4;
    var partialName = lastFocusCtrl.id.substring(mainWordStartsAt);

    switch (partialName) {
        case 'Hour':
            if ((parseFloat(lastFocusCtrl.value) == 12) && (selectedTimeFormat == 12)) // Accounts for 12/24 hour time loop, starts over at max
                lastFocusCtrl.value = '01';
            else if ((parseFloat(lastFocusCtrl.value) == 23) && (selectedTimeFormat == 24))
                lastFocusCtrl.value = '00';
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value) + 1);
            break;
        case 'Minute':
            if (parseFloat(lastFocusCtrl.value) > parseFloat(59 - MinuteIncrement))
                lastFocusCtrl.value = '00';
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value) + MinuteIncrement);
            break;
        case 'Second':
            if (parseFloat(lastFocusCtrl.value) > parseFloat(59 - SecondIncrement))
                lastFocusCtrl.value = '00';
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value) + SecondIncrement);
            break;
        case 'AmPm':
            if (lastFocusCtrl.value == 'AM')
                lastFocusCtrl.value = 'PM';
            else if (lastFocusCtrl.value == 'PM')
                lastFocusCtrl.value = 'AM';
            else
                lastFocusCtrl.value = 'PM';
            break;
    }
    lastFocusCtrl.focus();
}

function subtractTime(c, selectedTimeFormat, MinuteIncrement, SecondIncrement) {
    if (c != null)
        ensureFocus(c);
    var mainWordStartsAt = lastFocusCtrl.id.indexOf('_txt') + 4;
    var partialName = lastFocusCtrl.id.substring(mainWordStartsAt);

    switch (partialName) {
        case 'Hour':
            if ((parseFloat(lastFocusCtrl.value) == 1) && (selectedTimeFormat == 12))
                lastFocusCtrl.value = selectedTimeFormat;
            else if ((parseFloat(lastFocusCtrl.value) == 0) && (selectedTimeFormat == 24))
                lastFocusCtrl.value = selectedTimeFormat - 1;
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value - 1));
            break;
        case 'Minute':
            if ((parseFloat(lastFocusCtrl.value - MinuteIncrement)) < 0)
                lastFocusCtrl.value = paddZeros(parseFloat(60 - MinuteIncrement));
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value - MinuteIncrement));
            if (lastFocusCtrl.value == '0')
                lastFocusCtrl.value = '00'
            break;
        case 'Second':
            if ((parseFloat(lastFocusCtrl.value - SecondIncrement)) < 0)
                lastFocusCtrl.value = paddZeros(parseFloat(60 - SecondIncrement));
            else
                lastFocusCtrl.value = paddZeros(parseFloat(lastFocusCtrl.value - SecondIncrement));
            if (lastFocusCtrl.value == '0')
                lastFocusCtrl.value = '00'
            break;
        case 'AmPm':
            if (lastFocusCtrl.value == 'AM')
                lastFocusCtrl.value = 'PM';
            else if (lastFocusCtrl.value == 'PM')
                lastFocusCtrl.value = 'AM';
            else
                lastFocusCtrl.value = 'PM';
            break;
    }
    lastFocusCtrl.focus();
}

function keepAmPm() {
    if (AmPm.value == 'AM')
        AmPm.value = 'PM';
    else if (AmPm.value == 'PM')
        AmPm.value = 'AM';
    else
        AmPm.value = 'PM';
}

function intOnly(i, selectedTimeFormat) {
    var mainWordStartsAt = i.id.indexOf('_txt') + 4;
    var partialName = i.id.substring(mainWordStartsAt);

    if (i.value.length > 0) {
        i.value = i.value.replace(/[^\d]+/g, '00');
    }

    switch (partialName) {
        case 'Hour':
            if ((parseFloat(i.value) > 12) && (selectedTimeFormat == 12))
                i.value = '12'
            else if ((parseFloat(i.value) > 23) && (selectedTimeFormat == 24))
                i.value = '23'
            else if ((parseFloat(i.value) < 1) && (selectedTimeFormat == 12))
                i.value = '01';
            if ((parseFloat(i.value) < 0) && (selectedTimeFormat == 23))
                i.value = '00';
            break;
        case 'Minute':
            if (parseFloat(i.value) > 59)
                i.value = '59';
            else if (parseFloat(i.value) <= 0)
                i.value = '00';
            break;
        case 'Second':
            if (parseFloat(i.value) > 59)
                i.value = '59';
            else if (parseFloat(i.value) <= 0)
                i.value = '00';
            break;
    }
}


    //c, selectedTimeFormat, MinuteIncrement, SecondIncrement

    // 38 up, 40 down
function updownArrow(e, c, selectedTimeFormat, MinuteIncrement, SecondIncrement) {
    var keynum;
    var numcheck;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if (keynum == 38) { // up
        lastFocusCtrl = c;
        addTime(c, selectedTimeFormat, MinuteIncrement, SecondIncrement);
        return false;
    }

    if (keynum == 40) { // down
        lastFocusCtrl = c;
        subtractTime(c, selectedTimeFormat, MinuteIncrement, SecondIncrement);
        return false;
    }
}


/*

setDate()  	Sets the day of the month in a Date object (from 1-31)  	1  	2  	3
setFullYear() 	Sets the year in a Date object (four digits) 	1 	4 	4
setHours() 	Sets the hour in a Date object (from 0-23) 	1 	2 	3
setMilliseconds() 	Sets the milliseconds in a Date object (from 0-999) 	1 	4 	4
setMinutes() 	Set the minutes in a Date object (from 0-59) 	1 	2 	3
setMonth() 	Sets the month in a Date object (from 0-11) 	1 	2 	3
setSeconds() 	Sets the seconds in a Date object (from 0-59) 	1 	2 	3
setTime() 	Calculates a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 	

*/